import { ComponentType } from '../types/component';
import { FieldType } from '../types/model';
import { PageType, LayoutType } from '../types/page';
import type { Component, Page, Model } from '../types/index';

/**
 * 图书管理系统模板
 */

// 图书模型
export const bookModel: Model = {
  name: 'book',
  label: '图书',
  fields: [
    { name: 'id', type: FieldType.STRING, label: 'ID', required: true },
    { name: 'title', type: FieldType.STRING, label: '书名', required: true },
    { name: 'author', type: FieldType.STRING, label: '作者', required: true },
    { name: 'publisher', type: FieldType.STRING, label: '出版社' },
    { name: 'publishDate', type: FieldType.DATE, label: '出版日期' },
    { name: 'isbn', type: FieldType.STRING, label: 'ISBN', required: true },
    { name: 'price', type: FieldType.NUMBER, label: '价格' },
    { name: 'category', type: FieldType.STRING, label: '分类' },
    { name: 'description', type: FieldType.STRING, label: '描述' },
    { name: 'stockQuantity', type: FieldType.NUMBER, label: '库存数量', required: true }
  ]
};

// 列表页面组件
const listPageComponents: Component[] = [
  // 页面标题
  {
    id: 'header_title',
    name: '页面标题',
    type: ComponentType.TEXT,
    props: {
      text: '图书管理系统',
      style: { marginBottom: '20px', fontSize: '24px', fontWeight: 'bold' }
    },
    layout: { x: 0, y: 0, w: 12, h: 1, minW: 4, minH: 1 },
    events: []
  },
  // 搜索栏
  {
    id: 'search_bar',
    name: '搜索栏',
    type: ComponentType.SEARCH_BAR,
    props: {
      placeholder: '搜索图书...',
      style: { marginBottom: '16px' }
    },
    layout: { x: 0, y: 1, w: 6, h: 1, minW: 4, minH: 1 },
    events: []
  },
  // 添加按钮
  {
    id: 'add_button',
    name: '添加按钮',
    type: ComponentType.BUTTON,
    props: {
      text: '添加图书',
      type: 'primary',
      style: { marginBottom: '16px', marginLeft: '8px' }
    },
    layout: { x: 6, y: 1, w: 2, h: 1, minW: 2, minH: 1 },
    events: []
  },
  // 图书表格
  {
    id: 'books_table',
    name: '图书表格',
    type: ComponentType.TABLE,
    props: {
      columns: [
        { title: 'ID', dataIndex: 'id', key: 'id' },
        { title: '书名', dataIndex: 'title', key: 'title' },
        { title: '作者', dataIndex: 'author', key: 'author' },
        { title: '出版社', dataIndex: 'publisher', key: 'publisher' },
        { title: 'ISBN', dataIndex: 'isbn', key: 'isbn' },
        { title: '价格', dataIndex: 'price', key: 'price' },
        { title: '库存', dataIndex: 'stockQuantity', key: 'stockQuantity' },
        { 
          title: '操作', 
          key: 'action',
          render: 'text: "编辑 | 删除"' 
        }
      ],
      dataSource: [
        {
          id: '1',
          title: 'JavaScript高级程序设计',
          author: 'Nicholas C. Zakas',
          publisher: '人民邮电出版社',
          isbn: '9787115549662',
          price: 99,
          stockQuantity: 20
        },
        {
          id: '2',
          title: '深入理解TypeScript',
          author: 'Boris Cherny',
          publisher: '电子工业出版社',
          isbn: '9787121365416',
          price: 69,
          stockQuantity: 15
        }
      ],
      pagination: { pageSize: 10 },
      style: { width: '100%' }
    },
    layout: { x: 0, y: 2, w: 12, h: 8, minW: 6, minH: 4 },
    events: []
  }
];

// 详情/编辑页面组件
const detailPageComponents: Component[] = [
  // 页面标题
  {
    id: 'detail_header_title',
    name: '页面标题',
    type: ComponentType.TEXT,
    props: {
      text: '图书详情',
      style: { marginBottom: '20px', fontSize: '24px', fontWeight: 'bold' }
    },
    layout: { x: 0, y: 0, w: 12, h: 1, minW: 4, minH: 1 },
    events: []
  },
  // 返回按钮
  {
    id: 'back_button',
    name: '返回按钮',
    type: ComponentType.BUTTON,
    props: {
      text: '返回列表',
      style: { marginBottom: '16px' }
    },
    layout: { x: 0, y: 1, w: 2, h: 1, minW: 2, minH: 1 },
    events: []
  },
  // 图书信息表单
  {
    id: 'book_form',
    name: '图书表单',
    type: ComponentType.FORM,
    props: {
      layout: 'horizontal',
      labelCol: { span: 4 },
      wrapperCol: { span: 16 },
      fields: [
        { name: 'title', label: '书名', type: 'input', required: true },
        { name: 'author', label: '作者', type: 'input', required: true },
        { name: 'publisher', label: '出版社', type: 'input' },
        { name: 'publishDate', label: '出版日期', type: 'date-picker' },
        { name: 'isbn', label: 'ISBN', type: 'input', required: true },
        { name: 'price', label: '价格', type: 'input-number' },
        { name: 'category', label: '分类', type: 'select', options: [
          { label: '编程', value: 'programming' },
          { label: '文学', value: 'literature' },
          { label: '科学', value: 'science' }
        ]},
        { name: 'description', label: '描述', type: 'textarea' },
        { name: 'stockQuantity', label: '库存数量', type: 'input-number', required: true }
      ],
      style: { width: '100%' }
    },
    layout: { x: 0, y: 2, w: 12, h: 10, minW: 6, minH: 6 },
    events: []
  },
  // 保存按钮
  {
    id: 'save_button',
    name: '保存按钮',
    type: ComponentType.BUTTON,
    props: {
      text: '保存',
      type: 'primary',
      style: { marginRight: '8px' }
    },
    layout: { x: 4, y: 12, w: 2, h: 1, minW: 2, minH: 1 },
    events: []
  },
  // 取消按钮
  {
    id: 'cancel_button',
    name: '取消按钮',
    type: ComponentType.BUTTON,
    props: {
      text: '取消'
    },
    layout: { x: 6, y: 12, w: 2, h: 1, minW: 2, minH: 1 },
    events: []
  }
];

// 页面定义
export const pages: Record<string, Page> = {
  list: {
    id: 'list',
    name: '图书列表',
    path: '/books',
    type: PageType.LIST,
    layoutType: LayoutType.GRID,
    components: listPageComponents
  },
  detail: {
    id: 'detail',
    name: '图书详情',
    path: '/books/:id',
    type: PageType.DETAIL,
    layoutType: LayoutType.GRID,
    components: detailPageComponents
  }
};

// 导出完整模板
export default {
  name: '图书管理系统',
  description: '基础的图书管理系统，包含图书列表和详情页',
  models: {
    book: bookModel
  },
  pages
};
